package com.lxl.action;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.lxl.model.Post;
import com.lxl.model.Tieba;
import com.lxl.model.User;
import com.lxl.service.PostService;
import com.lxl.service.TiebaService;
import com.lxl.service.UserService;

/**
 * 帖子_添加帖子Action
 * 
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月9日 下午4:55:15
 */
@Controller
@Transactional
public class AddPostAction {
    @Resource
    PostService postService;// 注入帖子Service
    @Resource
    UserService userService;// 注入用户Service
    @Resource
    TiebaService tiebaService;// 注入贴吧Service
    private String userName;// 用户名
    private String tiebaName;// 贴吧名
    private String postTitle="";// 帖子标题
    private String postContent;// 帖子内容

    /**
     * 调用本Action时执行本方法
     * 
     * @return 用于Action判断跳转方向的字符串
     * @throws UnsupportedEncodingException 
     */
    public String execute() throws UnsupportedEncodingException {
        User user = userService.find(userName);
        Tieba tieba = tiebaService.find(tiebaName);
        Post post = new Post(postTitle, postContent, user, tieba);
        if(postTitle.getBytes("GBK").length<=20){            
            postService.add(post);
            return "success";
        }else{
            return "failureTitleTooLong";
        }        
    }

    /**
     * 获得用户名
     * 
     * @return 用户名
     */
    public String getUserName() {
        return userName;
    }

    /**
     * 设置用户名
     * 
     * @param userName
     *            用户名
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * 获得贴吧名
     * 
     * @return 贴吧名
     */
    public String getTiebaName() {
        return tiebaName;
    }

    /**
     * 设置贴吧名
     * 
     * @param tiebaName
     *            贴吧名
     */
    public void setTiebaName(String tiebaName) {
        this.tiebaName = tiebaName;
    }

    /**
     * 获得帖子标题
     * 
     * @return 帖子标题
     */
    public String getPostTitle() {
        return postTitle;
    }

    /**
     * 设置帖子标题
     * 
     * @param postTitle
     *            帖子标题
     */
    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }

    /**
     * 获得帖子内容
     * 
     * @return 帖子内容
     */
    public String getPostContent() {
        return postContent;
    }

    /**
     * 设置帖子内容
     * 
     * @param postContent
     *            帖子内容
     */
    public void setPostContent(String postContent) {
        this.postContent = postContent;
    }
}
